﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EM.Repository.utility;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Telerik.WebControls;

namespace EM.definition.finance.bank
{
    public partial class _default : System.Web.UI.Page
    {

        int pageId = 11;
        DbHelper srv = new DbHelper();
        FillCombo fc = new FillCombo();

        protected void Page_Load(object sender, EventArgs e)
        {
            Authentication ua = new Authentication();
            ua.CheckRolePageAuthentication(pageId, Authentication.RedirectionURLTypes.LoginPage);

            if (!Page.IsPostBack)
            {
                fc.LookUpValue(ddlActiveFLg, "", true, "SYS_YES_NO", Session["LANGCODE"].ToString());
                this.GetData();
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.GetData();
        }

        private void GetData()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT BANKID, BANKNAME, ACTIVEFLG FROM TBLBANK WHERE 1 = 1 ORDER BY BANKNAME ASC");

            SqlCommand cmd = new SqlCommand();

            if (txtBANKNAME.Text != "")
            {
                builder.Append(" AND BANKNAME LIKE @BANKNAME ");
                cmd.Parameters.AddWithValue("@BANKNAME", "%" + txtBANKNAME.Text + "%");
            }
            if (ddlActiveFLg.SelectedValue != "")
            {
                builder.Append(" AND ACTIVEFLG = @ACTIVEFLG ");
                cmd.Parameters.AddWithValue("@ACTIVEFLG", ddlActiveFLg.SelectedValue);
            }

            cmd.CommandText = builder.ToString();
            DataTable dt = srv.GetDataTable(cmd, "");
            rptBank.DataSource = dt;
            rptBank.DataBind();
        }

        protected void rptBank_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            DropDownList ddlACTIVE = e.Item.FindControl("ddlACTIVE") as DropDownList;
            if (e.Item.ItemType == ListItemType.Header)
            {
                fc.LookUpValue(ddlACTIVE, "", true, "SYS_YES_NO", Session["LANGCODE"].ToString());
            }
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                TextBox txtBANK = e.Item.FindControl("txtBANK") as TextBox;
                
                RequiredFieldValidator rfvBank = e.Item.FindControl("rfvBank") as RequiredFieldValidator;
                RequiredFieldValidator rfvActive = e.Item.FindControl("rfvActive") as RequiredFieldValidator;
                ValidationSummary vsUpdate = e.Item.FindControl("vsUpdate") as ValidationSummary;
                LinkButton btnUpdate = e.Item.FindControl("btnUpdate") as LinkButton;

                rfvBank.ValidationGroup = "vgUpdate_" + e.Item.ItemIndex;
                rfvActive.ValidationGroup = "vgUpdate_" + e.Item.ItemIndex;
                vsUpdate.ValidationGroup = "vgUpdate_" + e.Item.ItemIndex;
                btnUpdate.ValidationGroup = "vgUpdate_" + e.Item.ItemIndex;

                DataRowView row = e.Item.DataItem as DataRowView;

                txtBANK.Text = row["BANKNAME"].ToString();
                fc.LookUpValue(ddlACTIVE, row["ACTIVEFLG"].ToString(), true, "SYS_YES_NO",Session["LANGCODE"].ToString());
            }
        }

        protected void rptBank_ItemCommand(object sender, RepeaterCommandEventArgs e)
        {
            TextBox txtBANK = e.Item.FindControl("txtBANK") as TextBox;
            DropDownList ddlACTIVE = e.Item.FindControl("ddlACTIVE") as DropDownList;
            if (e.CommandName == "Insert")
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = @"INSERT INTO TBLBANK
                                    (BANKNAME, ACTIVEFLG, CREUSERID, CREDATE, MODUSERID, MODDATE)
                                    SELECT @BANKNAME, @ACTIVEFLG,  @CREUSERID, GETDATE(), @CREUSERID, GETDATE()";
                cmd.Parameters.AddWithValue("@BANKNAME", txtBANK.Text);
                cmd.Parameters.AddWithValue("@ACTIVEFLG", ddlACTIVE.SelectedValue);
                cmd.Parameters.AddWithValue("@CREUSERID", Session["USERID"]);
                srv.ExecuteNonQuery(cmd);
            }
            else if (e.CommandName == "Update")
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = @"UPDATE TBLBANK SET
                                    BANKNAME = @BANKNAME,
                                    ACTIVEFLG = @ACTIVEFLG,
                                    MODUSERID = @MODUSERID,
                                    MODDATE = GETDATE()
                                    WHERE BANKID = @BANKID";
                cmd.Parameters.AddWithValue("@BANKNAME", txtBANK.Text);
                cmd.Parameters.AddWithValue("@ACTIVEFLG", ddlACTIVE.SelectedValue);
               
                cmd.Parameters.AddWithValue("@MODUSERID", Session["USERID"]);
                cmd.Parameters.AddWithValue("@BANKID", e.CommandArgument);
                srv.ExecuteNonQuery(cmd);
            }
            else if (e.CommandName == "Delete")
            {
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = @"DELETE TBLBANK WHERE BANKID = @BANKID";
                cmd.Parameters.AddWithValue("@BANKID", e.CommandArgument);
                srv.ExecuteNonQuery(cmd);
            }

            this.GetData();
        }
    }
}